package com.demo.datastructure.linkedList;

/**
 * @description:  letcode  160 相交链表
 * @author: ljr
 * @time: 2021/4/15 14:50
 */
public class ListNode {

    int val;
    ListNode next;
    ListNode() {}
    ListNode(int val) { this.val = val; }
    ListNode(int val, ListNode next) { this.val = val; this.next = next; }

    public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
        if(headA == null || headB  == null){
            return null;
        }
        ListNode nextA = headA;
        ListNode nextB = headB;
        while(nextA != nextB){
            nextA = nextA == null ? headB : nextA.next;
            nextB = nextB == null ? headA : nextB.next;
        }
        return headA;
    }

}
